package com.example.hms.service.servicelmpl;
import com.example.hms.dao.RoomTypeDao;
import com.example.hms.entity.RoomType;
import com.example.hms.entity.SystemLog;
import com.example.hms.service.RoomTypeService;
import com.example.hms.service.SystemLogService;
import com.example.hms.util.Result;
import com.example.hms.util.ResultSubmit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
public class RoomTypeServiceImpl implements RoomTypeService {
    @Resource
    RoomTypeDao roomTypeDao;

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Resource
    SystemLogService systemLogService;

    @Override
    public Result selectAllRoomType( String roomTypeName,BigDecimal roomPrice) {
        List<RoomType> roomTypeList = roomTypeDao.selectAllRoomType( roomTypeName, roomPrice,null);
        return ResultSubmit.success(roomTypeList);
    }

    @Override
    public Result insert(RoomType roomType, HttpServletRequest request) {
        // 设置房间类型ID，使用当前系统时间的绝对值确保唯一性
        roomType.setRoomTypeId(Math.abs((int) System.currentTimeMillis()));
        // roomType.setRoomPrice();
        roomType.setDeleteIdentity("未删除");
        // 设置数据更新时间，使用当前系统时间
        roomType.setDataUpdateTime(format.format(new Date()));
        int insert1 = roomTypeDao.insert(roomType);

        // todo  系统记录
        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("roomType");
        // systemLog.setUseTableDataIdBefore(String.valueOf(user.getUserTableId()));

        systemLog.setUseTableDataIdAfter(String.valueOf(
                roomTypeDao.selectAllRoomType( roomType.getRoomTypeName(), roomType.getRoomPrice(), null).
                        get(0).getRoomTypeTableId()));
        systemLog.setUseType("房间类型新增");
        systemLogService.insert(systemLog);
        if (insert1 == 0) {
            // 事务回滚(手动回滚)
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("添加失败");
        } else {
            return ResultSubmit.success("添加成功");
        }
    }


    @Override
    public Result update(RoomType roomType, HttpServletRequest request) {
        int update1 = roomTypeDao.update(roomType.getRoomTypeId().toString());
        // int update1 = roomTypeDao.update(String.valueOf(roomType));
        roomType.setDeleteIdentity("未删除");
        roomType.setDataUpdateTime(format.format(new Date()));
        // 清除
        // roomType.setRoomTypeId(0);
        // 插入新数据
        int insert1 = roomTypeDao.insert(roomType);

        //todo 系统记录
        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("roomType");
        // systemLog.setUseTableDataIdBefore(String.valueOf(user.getUserTableId()));

        systemLog.setUseTableDataIdBefore(String.valueOf(
                roomTypeDao.selectAllRoomType( roomType.getRoomTypeName(),roomType.getRoomPrice(),null).
                        get(0).getRoomTypeTableId()));
        systemLog.setUseTableDataIdAfter(String.valueOf(
                roomTypeDao.selectAllRoomType(roomType.getRoomTypeName(), roomType.getRoomPrice(),null).
                        get(0).getRoomTypeTableId()));
        systemLog.setUseType("房间类型更新");
        systemLogService.insert(systemLog);
        if (update1 == 1 || insert1 == 1) {
            return ResultSubmit.success("修改成功");
        } else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("修改失败");
        }
    }


    @Override
    public Result delete(String roomTypeId, HttpServletRequest request) {

        Integer tableId = roomTypeDao.selectAllRoomType(null,null,
                roomTypeId).get(0).getRoomTypeTableId();

        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("roomType");
        systemLog.setUseTableDataIdBefore(String.valueOf(tableId));

        systemLog.setUseType("房间类型删除");
        systemLogService.insert(systemLog);

        int delete1 = roomTypeDao.update(roomTypeId);
//        System.out.println("删除执行后：" + delete1);

        if (delete1 != 0) {
            return ResultSubmit.success("删除成功");
        } else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("删除失败");
        }
    }
}
